package view;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import model.Group;
import model.Student;

/**
 *
 * @author cska
 *
 */

public class Lab_StudentDlg extends JDialog implements ocDialog {

    protected GridBagConstraints c;
    protected List<Student> students;
    protected Group group;
    public boolean fl;
    protected JPanel mainPanel;
    protected JScrollPane sp;
    protected List<JCheckBox> checks;
    protected JPanel bp1, bp2;
    protected JButton all, none;
    protected JButton ok, cancel;

    public Lab_StudentDlg(JFrame owner, boolean modal, String title, List<Student> students) {
        super(owner, title, modal);
        this.students = students;
        setLayout(new GridBagLayout());

        c = new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER,
                GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0);
        mainPanel = new JPanel(new GridBagLayout());
        sp = new JScrollPane(mainPanel);
        sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        add(sp,c);

        c = new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.NORTH,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0);
        checks = new ArrayList<JCheckBox>();
        for (int i = 0; i < this.students.size(); ++i) {
            c.gridy = i;
            checks.add(new JCheckBox(this.students.get(i).getFio(), true));
            mainPanel.add(checks.get(i),c);
        }
        c.weighty = 1.0;
        c.gridy = this.students.size();
        mainPanel.add(new JPanel(),c);

        c = new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0);
        
        bp1 = new JPanel(new GridBagLayout());
        add(bp1,c);
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 1;
        c.anchor = GridBagConstraints.EAST;
        c.fill = GridBagConstraints.CENTER;
        all = new JButton("Выделить все");
        all.addActionListener(new allAdapter(this));
        bp1.add(all,c);
        c.gridx = 1;
        c.weightx = 0.0;
        none = new JButton("Снять выделение");
        none.addActionListener(new noneAdapter(this));
        bp1.add(none,c);

        c = new GridBagConstraints(0, 2, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0);
        
        bp2 = new JPanel(new GridBagLayout());
        add(bp2,c);
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 1;
        c.anchor = GridBagConstraints.EAST;
        c.fill = GridBagConstraints.CENTER;
        ok = new JButton("Сохранить");
        ok.addActionListener(new okAdapter(this));
        bp2.add(ok,c);
        c.gridx = 1;
        c.weightx = 0.0;
        cancel = new JButton("Отмена");
        cancel.addActionListener(new cancelAdapter(this));
        bp2.add(cancel,c);

        setSize(400,320);
        Dimension screen = getToolkit().getScreenSize().getSize();
        setLocation((screen.width-400)/2, (screen.height-320)/2);
        setResizable(false);
        setDefaultCloseOperation(HIDE_ON_CLOSE);
        setVisible(true);
    }

    public List<Student> getStudents() {
        List<Student> s = new ArrayList<Student>();
        for (int i = 0; i < students.size(); ++i)
            if (checks.get(i).isSelected())
                s.add(students.get(i));
        return s;
    }

    public void ok() {
        fl = true;
        setVisible(false);
    }

    public void cancel() {
        fl = false;
        setVisible(false);
    }

    public void selectAll() {
        for (JCheckBox box : checks)
            box.setSelected(true);
    }

    public void selectNone() {
        for (JCheckBox box : checks)
            box.setSelected(false);
    }

    class allAdapter implements ActionListener {

        protected Lab_StudentDlg lsd;

        public allAdapter(Lab_StudentDlg lsd) {
            this.lsd = lsd;
        }

        public void actionPerformed(ActionEvent e) {
            lsd.selectAll();
        }

    }

    class noneAdapter implements ActionListener {

        protected Lab_StudentDlg lsd;

        public noneAdapter(Lab_StudentDlg lsd) {
            this.lsd = lsd;
        }

        public void actionPerformed(ActionEvent e) {
            lsd.selectNone();
        }

    }

}
